package com.css.fxfzfxqh.modules.synthesis.controller;

import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.singleIndicatorRiskZoneSet.model.vo.SingleIndicatorRiskZoneSetSaveVo;
import com.css.fxfzfxqh.modules.synthesis.entity.PopulationAndLoss;
import com.css.fxfzfxqh.modules.synthesis.param.CheckParam;
import com.css.fxfzfxqh.modules.synthesis.param.QueryParam;
import com.css.fxfzfxqh.modules.synthesis.param.StatisticsParam;
import com.css.fxfzfxqh.modules.synthesis.service.AssessService;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 综合风险评估
 */
@Slf4j
@RestController
@RequestMapping("synthesis")
public class AssessController {

    @Autowired
    private AssessService assessService;
    //判断当前任务是否可以进行评估 可以返回true 不可以返回false
    @PostMapping("check")  //参数 任务id
    public RestResponse check(@RequestBody CheckParam param) {
        try {
            Boolean check = assessService.check(param);
            return RestResponse.succeed(check);
        } catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail("操作失败");
        }
    }

    //计算区划值
    @PostMapping("count")
    public RestResponse count(@RequestBody CheckParam param) {
        try {
            assessService.count(param);
            return RestResponse.succeed();
        } catch (RuntimeException e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail(e1.getMessage());
        }catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            return RestResponse.fail("操作失败");
        }
    }

    //查询列表
    @GetMapping("getResults")
    public RestResponse getResults(HttpServletRequest request, QueryParam param) {
        RestResponse restResponse = null;
        try {
            int page = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int size = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> data = assessService.getResults(param, page, size);
            restResponse = RestResponse.succeed(data);
        }catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            restResponse = RestResponse.fail("查询列表失败");
        }
        return restResponse;
    }

    //统计
    @GetMapping("statistics")
    public RestResponse statistics(QueryParam param) {
        RestResponse restResponse = null;
        try {
            List<StatisticsParam> params = assessService.statistics(param);
            restResponse = RestResponse.succeed(params);
        }catch (Exception e1) {
            log.error(e1.getMessage(), e1);
            restResponse = RestResponse.fail("统计列表失败");
        }
        return restResponse;
    }
}
